package com.xianan.xuhui.netsecurityproject.interceptor;

import com.xianan.xuhui.netsecurityproject.domain.enums.WebClientTypeEnum;
import com.xianan.xuhui.netsecurityproject.domain.token.WebSessionDto;
import com.xianan.xuhui.netsecurityproject.exception.BizException;
import com.xianan.xuhui.netsecurityproject.exception.BizExceptionEnum;
import com.xianan.xuhui.netsecurityproject.utils.WebSessionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xiaoli
 * @description TODO
 * @since 2025/3/25 1:25
 */
@Slf4j
@Component
public class WebBindPhoneInterceptor implements HandlerInterceptor {
    /**
     * 公众号/小程序判断是否绑定手机号
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Integer clientType = WebSessionUtil.getClientType();
        if(WebClientTypeEnum.WX_MINI_PROGRAM.getKey().equals(clientType)){
            WebSessionDto webSessionDto = WebSessionUtil.get();
            if(null==webSessionDto){
                throw new BizException(BizExceptionEnum.EMP_UNBIND_TEL);
            }
            String openId = webSessionDto.getOpenId();
            Long id = webSessionDto.getId();
            //公众号或小程序未绑定手机号，提示绑定手机号
            if(StringUtils.hasText(openId) && null==id){
                throw new BizException(BizExceptionEnum.EMP_UNBIND_TEL);
            }
        }

        return true;
    }
}
